//
//  SEGLouDongListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-1-7.
//  Copyright (c) 2014年 陈区. All rights reserved.
//


#import "SEGLouDongListViewController.h"
#import "SEGDanYuanListViewController.h"
#import "SEGFangHaoListViewController.h"


#import "SEGHouseSelectCell.h"

@interface SEGLouDongListViewController ()<UITableViewDelegate,UITableViewDataSource>


/// 表格
@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) SEGLouDongListViewModel *viewModel;
@end

@implementation SEGLouDongListViewController
#pragma mark - life cycle
- (instancetype)initWithViewModel:(SEGLouDongListViewModel*)viewModel{
    if (self == [super init]) {
        _viewModel = viewModel;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self showCustomNavigationBackButton];
    self.title = @"选择楼栋";
    
    [self segInitViews];
    [self bindViewModel];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel.myProfileManager.seg_segcommunitybuildlistmodel count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SEGHouseSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:kSEGHouseSelectCellIdentifier forIndexPath:indexPath];
    if (self.viewModel.myProfileManager.seg_segcommunitybuildlistmodel.count > indexPath.row) {
        SEGCommunityBuildListModel* model = [self.viewModel.myProfileManager.seg_segcommunitybuildlistmodel objectAtIndex:indexPath.row];
        cell.houseInfoText = model.name;
        if ([model.buildId isEqualToString:self.viewModel.selectedModel.buildId]) {
            cell.nameLabel.textColor = kColor_T;
        } else {
            cell.nameLabel.textColor = kColor_1;
        }
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 显示选中状态
    self.viewModel.selectedModel = self.viewModel.myProfileManager.seg_segcommunitybuildlistmodel[indexPath.row];
    [self.tableView reloadData];
    // 查询是否有单元数据
    [self.viewModel.unitDataCommand execute:self.viewModel.selectedModel.buildId];
    [SVProgressHUD show];
}

#pragma mark - Init Views
-(void)segInitViews{
    [self.view addSubview:self.tableView];
}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    //TODO: RAC拆包,NEED TEST!
    @weakify(self)
    [[[self.viewModel.requestDataCommand executionSignals] switchToLatest] subscribeNext:^(id  _Nullable x) {
        NSLog(@"executionSignals");
        @strongify(self)
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
    [self.viewModel.requestDataCommand execute:nil];
    [SVProgressHUD show];
    
    [[[self.viewModel.unitDataCommand executionSignals] switchToLatest] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        [SVProgressHUD dismiss];
        
        NSLog(@"unitDataCommand executionSignals");
        if (self.viewModel.myProfileManager.seg_segunitbybuildidmodel.count > 0) { // 有单元数据, 去单元列表
            SEGDanYuanListViewModel *danYuanListViewModel = [[SEGDanYuanListViewModel alloc] init];
            danYuanListViewModel.popClassName = self.viewModel.popClassName;
            danYuanListViewModel.unitModelArray = self.viewModel.myProfileManager.seg_segunitbybuildidmodel;
            danYuanListViewModel.buildModel = self.viewModel.selectedModel;
            if (self.viewModel.unitModel) danYuanListViewModel.selectedModel = self.viewModel.unitModel;
            if (self.viewModel.houseModel) danYuanListViewModel.houseModel = self.viewModel.houseModel;
            
            SEGDanYuanListViewController *myProfileViewCtl = [[SEGDanYuanListViewController alloc] initWithViewModel:danYuanListViewModel];
            myProfileViewCtl.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myProfileViewCtl animated:YES];
        } else {
            SEGFangHaoListViewModel *fangHaoListViewModel = [[SEGFangHaoListViewModel alloc] init];
            fangHaoListViewModel.popClassName = self.viewModel.popClassName;
            fangHaoListViewModel.buildModel = self.viewModel.selectedModel;
            if (self.viewModel.houseModel) fangHaoListViewModel.selectedModel = self.viewModel.houseModel;
            
            SEGFangHaoListViewController *myProfileViewCtl = [[SEGFangHaoListViewController alloc] initWithViewModel:fangHaoListViewModel];
            myProfileViewCtl.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:myProfileViewCtl animated:YES];
        }
    }];
    
    [self.viewModel.messageDataSubject subscribeNext:^(NSString *  _Nullable x) {
        [SEGAlertView showTmepMessageWithMessage:x];
    }];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        CGRect appbounds = CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight);
        _tableView = [[UITableView alloc] initWithFrame:appbounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[SEGHouseSelectCell class] forCellReuseIdentifier:kSEGHouseSelectCellIdentifier];
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}

#pragma mark setter
@end
